package com.qf.flash.mvp.presenter;

import android.app.Activity;
import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.qf.dblib.dao.entity.LocationBean;
import com.qf.dblib.dao.impl.LocationDao;
import com.qf.flash.mvp.contract.LocationContract;
import com.qf.flashlib.base.BasePresenter;

/**
 * 定位
 * 2020/2/26 17:03
 */
public class LocationPresenterImpl extends BasePresenter<LocationContract.View> implements LocationContract.Presenter {
    /* 单次定位 */
    private boolean isOnceStarted = false;
    private AMapLocationClient mOnceLocationClient = null;
    private AMapLocationListener mOnceLocationListener = new OnceAMapLocationListener();


    private Context mContext;

    public LocationPresenterImpl(Activity context) {
        this.mContext = context;
    }

    /**
     * 开启单次定位
     */
    @Override
    public void startOncetLocation() {
        if (isOnceStarted)
            return;
        if (mOnceLocationClient == null)
            initOnceLocation();
        mOnceLocationClient.startLocation();
        isOnceStarted = true;
    }


    /**
     * 停止单次定位
     */
    private void stopOnceLocation() {
        if (mOnceLocationClient != null) {
            mOnceLocationClient.stopLocation();
            mOnceLocationClient.onDestroy();
            mOnceLocationClient = null;
            isOnceStarted = false;
        }
    }


    /**
     * 初始化单次定位
     */
    private void initOnceLocation() {
        //初始化定位
        mOnceLocationClient = new AMapLocationClient(mContext);
        mOnceLocationClient.setLocationListener(mOnceLocationListener);
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);      //高精度模式
//        mLocationOption.setInterval(INTERVAL_TIME);                                                     //设置定位间隔
        mLocationOption.setOnceLocation(true);                                                          //获取一次定位结果
        mLocationOption.setOnceLocationLatest(true);                                                    //获取最近3s内精度最高的一次定位结果
        mLocationOption.setLocationCacheEnable(false);                                                  //关闭缓存
        mLocationOption.setMockEnable(false);                                                           //不允许模拟位置
        mOnceLocationClient.setLocationOption(mLocationOption);
    }

    /**
     * 单次定位监听
     */
    private class OnceAMapLocationListener implements AMapLocationListener {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            stopOnceLocation();
            if (aMapLocation != null) {
                if (mRootView != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        LocationBean locationBean = new LocationBean();
                        locationBean.setAdCode(aMapLocation.getAdCode());
                        locationBean.setCity(aMapLocation.getCity());
                        locationBean.setCountry(aMapLocation.getCountry());
                        locationBean.setProvince(aMapLocation.getProvince());
                        locationBean.setLat(aMapLocation.getLatitude());
                        locationBean.setLng(aMapLocation.getLongitude());
                        LocationDao.getInstance().saveLocation(locationBean);
                        mRootView.onLocationSuccess(aMapLocation);
                    } else {
                        mRootView.onLocationFailed(aMapLocation.getErrorInfo());
                    }

                }
            }
        }
    }


    @Override
    public void detachView() {
        super.detachView();
        this.mRootView = null;
        stopOnceLocation();
    }
}
